<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户列表</title>
    <script src="js/vue.js"></script>
    <script src="js/axios.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        .table {
            width: 400px;
        }

        .table .item {
            float: left;
            width: 100px;
            height: 40px;
            text-align: center;
            line-height: 40px;
            background-color: black;
            color: burlywood;
        }
    </style>
</head>

<body>
    <div id="app">
        <div>
            <div>用户名<input v-model="username" /></div>
            <div>密码<input v-model="userpwd" /></div>
            <button @click="add">新增</button>
        </div>
        <div class="table">
            <div class="tr">
                <div class="item">编号</div>
                <div class="item">用户名</div>
                <div class="item">密码</div>
                <div class="item">操作</div>
            </div>
            <div class="tr" v-for="item in list">
                <div class="item">{{item.id}}</div>
                <div class="item">{{item.username}}</div>
                <div class="item">{{item.userpwd}}</div>
                <div class="item" @click="del(item.id)">删除</div>
            </div>
        </div>

    </div>
</body>
<script>
    new Vue({
        el: "#app",
        data: {
            username: "",
            userpwd: "",
            list: [],//用户集合

        },
        methods: {
            async add() {

                //判断用户名是否存在(查询)
                let { data: list } = await axios({
                    method: "get",
                    url: "http://localhost:2346/userinfos",
                    params: {
                        username: this.username
                    }
                })
                //查询用户名是否存在
                if (list.length) {
                    alert("该用户名已经存在");
                    return false;
                }

                let { data } = await axios({
                    method: "post",
                    url: "http://localhost:2346/userinfos",
                    data: {
                        username: this.username,
                        userpwd: this.userpwd
                    }
                })
                //新增
                this.list.push(data);
            },
            async del(id) {
                await axios({
                    method: "delete",
                    url: "http://localhost:2346/userinfos/" + id
                })

                this.list = this.list.filter(item => item.id != id);


            }

        },
        async created() {
            let { data: list } = await axios({
                method: "get",
                url: "http://localhost:2346/userinfos",
                params: {}
            })
            this.list = list;
        }


    })
  

</script>

</html>